<template>
  <div class="app-container">
    <!-- ****************************** -->
    <el-row
      :gutter="20"
      class="header"
      style="margin-left: 0;"
    >
      <div class="prompt">
        <h1>日常检查单</h1>
        <div class="prompt-main">
          <el-col :span="24">
            <div>
              <p class="text1">{{ ass }}</p>
              <h2>今日已检查特种车辆</h2>
            </div>
          </el-col>
        </div>
      </div> 
      <!-- 查询表单 -->
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item
          label="车型"
          prop="applicableModels"
        >
          <el-select
            filterable
            v-model="queryParams.applicableModels"
            placeholder="所有项"
            clearable
            @change="currentCarType"
          >
            <el-option
              v-for="item in carData"
              :key="item.id"
              :label="item.applicableModels"
              :value="item.applicableModels"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="车号"
          prop="wagonNumber"
        >
          <el-select
            filterable
            v-model="queryParams.wagonNumber"
            placeholder="所有项"
            clearable
          >
            <el-option
              v-for="item in currtentCarNumber"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="检查日期"
          prop="dateView"
        >
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd HH:mm:ss.00"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
       
        <el-form-item class="header-button">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >查询</el-button>
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >添加</el-button>
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['monitor:logininfor:export']"
          >导出</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 数据展示-->
    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="menuList"
      row-key="menuId"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      style="width: 100%"
    >
      <el-table-column
        prop="id"
        label="ID"
        align="center"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="applicableModels"
        label="车型"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="wagonNumber"
        label="车号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="inspectDate"
        label="检查日期"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="signatureOfInspector"
        label="检查人"
        align="center"
      ></el-table-column>
      <el-table-column
          label="摩托小时数"
          prop="vehicleMotorcycleHours"
        >
          
        </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        align="center"
      ></el-table-column>
      <el-table-column
        label="操作"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="goToDetail(scope.row)"
            v-hasPermi="['monitor:operlog:query']"
          >详细</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:menu:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 表单详细弹窗start -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="open"
      width="700px"
      append-to-body
      :before-close="dialogClose"
    >
      <div>
        <div
          v-for="(item, index) in form"
          :key="index"
          class="row"
        >
          <div>
            {{ index + 1 }}.{{ item.subject }}
          </div>
          <el-radio
            disabled
            v-model="item.isAbnormal"
            label="1"
          >正常</el-radio>
          <el-radio
            disabled
            v-model="item.isAbnormal"
            label="0"
          >异常&nbsp;{{  item.content[0].text  }}</el-radio>
          <el-radio
            disabled
            v-model="item.isAbnormal"
            label="2"
            v-if="(sType == 19 || sType == 18) && index === 9"
          >未安装</el-radio>
        </div>
        <el-input
          v-if="sType != 19 && sType != 18"
          placeholder="车辆摩托小时"
          v-model="vehicleMotorcycleHours"
          :disabled="true"
        > <template slot="prepend">车辆摩托小时：</template></el-input>
        <el-input
          v-if="sType != 19 && sType != 18"
          placeholder="补加油量"
          v-model="refuelingVolume"
          :disabled="true"
        > <template slot="prepend">补加油量：</template></el-input>
        <el-input
          placeholder="备注"
          v-model="remarks"
          :disabled="true"
        > <template slot="prepend">备注：</template></el-input>
      </div>
    </el-dialog>
    <!-- 表单详细弹窗end -->
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect";
import { checkList, getCarData, deleteinfieldOrder, inspectQueryById, queryById , getInfieldSpecialVehicleCount} from "@/api/system/check";
import { downloadExcel } from "@/api/download"


export default {
  name: "Menu",
  dicts: ['sys_show_hide', 'sys_normal_disable'],
  components: { Treeselect, IconSelect },
  data () {
    return {
      /* 日期 start*/
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 日期范围
      dateRange: [],
      /* 日期 end*/
      //车辆信息
      carData: [],
      //当前车型的车号
      currtentCarNumber: [],
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      menuList: [],
      // 菜单树选项
      menuOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applicableModels: "",
        dateView: "",
        isMe: false,
        type: 0,
        typeName: "",
        wagonNumber: "",
        startTime: "",
        endTime: ""
      },

      // 表单参数
      dialogTitle: '',
      formList: {},
      form: [],
      formTemp: [],
      a1: {},
      vehicleMotorcycleHours: 0,
      refuelingVolume: 0,
      remarks: '',
      loading: false,
      ass:"",
      sType: ''
    };
  },
  computed: {
    isLook () {
      return true
      // return this.$route.query.source === 'edit'
    },
  },
  created () {
    this.getList();
    this.geta();
    this.getCarData()
  },
  activated () {
    this.getList();
    this.getCarData()
  },
  methods: {
    geta(){
      getInfieldSpecialVehicleCount().then(response => {
        this.ass = response.data
      });
    },
    // 选择图标
    selected (name) {
      this.form.icon = name;
    },
    /** 查询菜单列表 */
    getList () {
      this.loading = true;
      /*start 请求数据转换*/
      this.queryParams.startTime = this.dateRange[0]
      this.queryParams.endTime = this.dateRange[1]
      checkList(this.queryParams).then(response => {
        this.menuList = this.handleTree(response.data.records, "id");
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 查询车辆数据 */
    getCarData () {
      getCarData().then(response => {
        this.carData = response.data
      });
    },
    /** 转换菜单数据结构 */
    normalizer (node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.menuId,
        label: node.menuName,
        children: node.children
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeselect () {
      response().then(response => {
        this.menuOptions = [];
        const menu = { menuId: 0, menuName: '主类目', children: [] };
        menu.children = this.handleTree(response.data, "id");
        this.menuOptions.push(menu);
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    //下拉框更新的车号
    currentCarType () {
      this.queryParams.wagonNumber = ''
      this.currtentCarNumber = this.carData.filter(item => {
        return item.applicableModels == this.queryParams.applicableModels
      })[0].applicableModelsData
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
     /** 搜索按钮操作 */
    handleAdd () {
      this.$router.push({
        path: "/user/inselect",
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      this.$modal.confirm('是否确认删除ID为"' + row.id + '"的数据项？').then(function () {
        return deleteinfieldOrder(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      downloadExcel('/infield-vehicle-equipment-inspect/export', `检查单_${new Date().getTime()}.xlsx`, {
        ...this.queryParams
      }, 'post')
    },
    // 查看详情页
    goToDetail (item) {
      this.formTemp = []
      console.log(item)
      this.open = true;
      this.dialogTitle = `ID为 ${item.id} 的检查单详情`
      // this.loading=true
      inspectQueryById({ id: item.type }).then((res) => {
        this.a1 = res.data;
        this.formList = res.data.content;
        this.sType = res.data.type;
        this.formList.forEach((i, sindex) => {
          this.formTemp.push({
            isAbnormal: '',
            index: i.index,
            content: this.getContentList(i.contentNumber),
            contentNumber: i.contentNumber,
            contentType: i.contentType,
            subject: i.subject,
            isShowText: i.contentType === 2 ? true : false,
          })
        })
        queryById({ id: item.id }).then((res) => {
          const intersection = [];
          const item = res.data.inspectTerm
          for (let object of this.formTemp) {
            if (item.some((otherObject) => object.index === otherObject.index)) {
              intersection.push(object)
            }
          }
          item.forEach((item, index) => {
            intersection[index].isAbnormal = `${item.isAbnormal}`
            this.getContentEdit(intersection[index].content, item.content);
             if (intersection[index].contentType !== 2) {
              intersection[index].isShowText = item.isAbnormal === 0;
             }
          });
          this.form = intersection;
          this.vehicleMotorcycleHours = res.data.vehicleMotorcycleHours;
          this.refuelingVolume = res.data.refuelingVolume;
          this.remarks = res.data.remarks;
          // this.loading = false;
        }).catch((e) => {
          console.log(e)
          console.log('error1')
          // this.loading = false;
        })
      }).catch(() => {
        console.log('error2')
        // this.loading = false;
      })
    },
    //详情页逻辑
    getContentList (num) {
      let arr = [];
      for (let i = 0; i < num; i++) {
        arr.push({ text: '' })
      }
      return arr
    },
    getContentEdit (con1, con2) {
      con1.forEach((item, index) => {
        item.text = con2[index]
      })
    },
    // 关闭详情页回调
    dialogClose (done) {
      this.formList = {}
      this.form = []
      done();
    }
  }
};
</script>

<style lang="scss" scoped>
.header {
  display: flex;
  // height: 240px;

  .prompt {
    display: flex;
    flex-direction: column;
    width: 800px;
    background-color: #4288e8;
    color: white;
    border-radius: 15px;

    h1 {
      text-align: center;
      font-size: 30px;
      letter-spacing: 4px;
    }

    .prompt-main {
      display: flex;
      justify-content: center;

      .text1 {
        font-size: 30px;
        font-weight: 20px;
        margin: 5px 0;
      }

      .text2 {
        font-size: 30px;
        font-weight: 20px;
        color: #c0c0c0;
        margin: 5px 0;
      }

      .vertical-line {
        height: 60px;
        width: 1px;
        background-color: #7b8189;
      }

      .el-col {
        text-align: center;
      }
    }
  }

  .el-form {
    margin-left: 10px;
    display: flex;
    // flex-direction: column;
    flex-wrap: wrap;

    .el-form-item {
      margin-bottom: 10px;
    }

    .el-form-item__label {
      width: 100px;
    }

    .header-button {
    }
  }
}

.row {
  margin-bottom: 10px;
  border-bottom: 1px #f5f7fa solid;
}

.prompt {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 100%;
    background-color: #3f8cda;
    color: white;
    border-radius: 15px;

    h1 {
      text-align: center;
      font-size: 30px;
      letter-spacing: 4px;
    }

    .prompt-main {
      display: flex;
      justify-content: center;

      .vertical-line {
        height: 60px;
        width: 1px;
        background-color: #7b8189;
      }

      .el-col {
        text-align: center;
      }

      .text1 {
        font-size: 30px;
        font-weight: 20px;
        color: #0fec76;
        margin: 5px 0;
      }

      .text2 {
        font-size: 30px;
        font-weight: 20px;
        color: #7a8014;
        margin: 5px 0;
      }

      .text3 {
        font-size: 30px;
        font-weight: 20px;
        color: #bac1d8;
        margin: 5px 0;
      }
    }
  }
</style>